#include <json/json.h>

#include "CnfSvrProtoHelper.h"

string build_response_get_all_cnf(NS_CNF ns, const list<MDS_CNF> l_mds, const list<DS_CNF> l_ds) {
	json_object * j = json_object_new_object();
        json_object * j_result = json_object_new_int(0);
        json_object_object_add(j, "RESULT", j_result);

	json_object * j_ns_cnf = json_object_new_object();
	json_object * j_ns_ip = json_object_new_string(ns.ip.c_str());
	json_object * j_ns_port = json_object_new_int(ns.port);
	json_object * j_ns_status = json_object_new_int(ns.status);
	json_object * j_ns_thread_num = json_object_new_int(ns.thread_num);
	json_object * j_ns_socket_conn_timeout = json_object_new_int(ns.socket_conn_timeout);
	json_object * j_ns_socket_idle_timeout = json_object_new_int(ns.socket_idle_timeout);
	json_object * j_ns_socket_send_timeout = json_object_new_int(ns.socket_send_timeout);
	json_object * j_ns_socket_recv_timeout = json_object_new_int(ns.socket_recv_timeout);
	json_object * j_ns_socket_max_request_size = json_object_new_int(ns.socket_max_request_size);
	json_object * j_ns_socket_max_response_size = json_object_new_int(ns.socket_max_response_size);
	json_object * j_ns_client_conn_pool_size = json_object_new_int(ns.client_conn_pool_size);
	json_object * j_ns_log_cnf_file = json_object_new_string(ns.log_cnf_file.c_str());
	json_object * j_ns_root_data_path = json_object_new_string(ns.root_data_path.c_str());
	json_object * j_ns_create_time = json_object_new_int(ns.create_time);
	json_object_object_add(j_ns_cnf, "IP", j_ns_ip);
	json_object_object_add(j_ns_cnf, "PORT", j_ns_port);
	json_object_object_add(j_ns_cnf, "STATUS", j_ns_status);
	json_object_object_add(j_ns_cnf, "THREAD_NUM", j_ns_thread_num);
	json_object_object_add(j_ns_cnf, "SOCKET_CONN_TIMEOUT", j_ns_socket_conn_timeout);
	json_object_object_add(j_ns_cnf, "SOCKET_IDLE_TIMEOUT", j_ns_socket_idle_timeout);
	json_object_object_add(j_ns_cnf, "SOCKET_SEND_TIMEOUT", j_ns_socket_send_timeout);
	json_object_object_add(j_ns_cnf, "SOCKET_RECV_TIMEOUT", j_ns_socket_recv_timeout);
	json_object_object_add(j_ns_cnf, "SOCKET_MAX_REQUEST_SIZE", j_ns_socket_max_request_size);
	json_object_object_add(j_ns_cnf, "SOCKET_MAX_RESPONSE_SIZE", j_ns_socket_max_response_size);
	json_object_object_add(j_ns_cnf, "CLIENT_CONN_POOL_SIZE", j_ns_client_conn_pool_size);
	json_object_object_add(j_ns_cnf, "LOG_CNF_FILE", j_ns_log_cnf_file);
	json_object_object_add(j_ns_cnf, "ROOT_DATA_PATH", j_ns_root_data_path);
	json_object_object_add(j_ns_cnf, "CREATE_TIME", j_ns_create_time);
	json_object_object_add(j, "NS_CNF", j_ns_cnf);
	
	json_object * j_mds_arr = json_object_new_array();
	for (list<MDS_CNF>::const_iterator iter = l_mds.begin(); l_mds.end() != iter; iter++) {
		json_object * jt = json_object_new_object();
		json_object * j_ip = json_object_new_string(iter->ip.c_str());
		json_object * j_port = json_object_new_int(iter->port);
		json_object * j_id = json_object_new_int(iter->id);
		json_object * j_status = json_object_new_int(iter->status);
		json_object * j_weight = json_object_new_int(iter->weight);
		json_object * j_thread_num = json_object_new_int(iter->thread_num);
		json_object * j_socket_conn_timeout = json_object_new_int(iter->socket_conn_timeout);
		json_object * j_socket_idle_timeout = json_object_new_int(iter->socket_idle_timeout);
		json_object * j_socket_send_timeout = json_object_new_int(iter->socket_send_timeout);
		json_object * j_socket_recv_timeout = json_object_new_int(iter->socket_recv_timeout);
		json_object * j_socket_max_request_size = json_object_new_int(iter->socket_max_request_size);
		json_object * j_socket_max_response_size = json_object_new_int(iter->socket_max_response_size);
		json_object * j_client_conn_pool_size = json_object_new_int(iter->client_conn_pool_size);
		json_object * j_log_cnf_file = json_object_new_string(iter->log_cnf_file.c_str());
		json_object * j_root_data_path = json_object_new_string(iter->root_data_path.c_str());
		json_object * j_create_time = json_object_new_int(iter->create_time);
		json_object_object_add(jt, "IP", j_ip);
		json_object_object_add(jt, "PORT", j_port);
		json_object_object_add(jt, "ID", j_id);
		json_object_object_add(jt, "STATUS", j_status);
		json_object_object_add(jt, "WEIGHT", j_weight);
		json_object_object_add(jt, "THREAD_NUM", j_thread_num);
		json_object_object_add(jt, "SOCKET_CONN_TIMEOUT", j_socket_conn_timeout);
		json_object_object_add(jt, "SOCKET_IDLE_TIMEOUT", j_socket_idle_timeout);
		json_object_object_add(jt, "SOCKET_SEND_TIMEOUT", j_socket_send_timeout);
		json_object_object_add(jt, "SOCKET_RECV_TIMEOUT", j_socket_recv_timeout);
		json_object_object_add(jt, "SOCKET_MAX_REQUEST_SIZE", j_socket_max_request_size);
		json_object_object_add(jt, "SOCKET_MAX_RESPONSE_SIZE", j_socket_max_response_size);
		json_object_object_add(jt, "CLIENT_CONN_POOL_SIZE", j_client_conn_pool_size);
		json_object_object_add(jt, "LOG_CNF_FILE", j_log_cnf_file);
		json_object_object_add(jt, "ROOT_DATA_PATH", j_root_data_path);
		json_object_object_add(jt, "CREATE_TIME", j_create_time);
		json_object_array_add(j_mds_arr, jt);
	}
	json_object_object_add(j, "MDS_CNFS", j_mds_arr);

	json_object * j_ds_arr = json_object_new_array();
	for (list<DS_CNF>::const_iterator iter = l_ds.begin(); l_ds.end() != iter; iter++) {
		json_object * jt = json_object_new_object();
		json_object * j_ip = json_object_new_string(iter->ip.c_str());
		json_object * j_port = json_object_new_int(iter->port);
		json_object * j_id = json_object_new_int(iter->id);
		json_object * j_status = json_object_new_int(iter->status);
		json_object * j_mds_id= json_object_new_int(iter->mds_id);
		json_object * j_read_weight = json_object_new_int(iter->read_weight);
		json_object * j_write_weight = json_object_new_int(iter->write_weight);
		json_object * j_thread_num = json_object_new_int(iter->thread_num);
		json_object * j_socket_conn_timeout = json_object_new_int(iter->socket_conn_timeout);
		json_object * j_socket_idle_timeout = json_object_new_int(iter->socket_idle_timeout);
		json_object * j_socket_send_timeout = json_object_new_int(iter->socket_send_timeout);
		json_object * j_socket_recv_timeout = json_object_new_int(iter->socket_recv_timeout);
		json_object * j_socket_max_request_size = json_object_new_int(iter->socket_max_request_size);
		json_object * j_socket_max_response_size = json_object_new_int(iter->socket_max_response_size);
		json_object * j_client_conn_pool_size = json_object_new_int(iter->client_conn_pool_size);
		json_object * j_log_cnf_file = json_object_new_string(iter->log_cnf_file.c_str());
		json_object * j_root_data_path = json_object_new_string(iter->root_data_path.c_str());
		json_object * j_create_time = json_object_new_int(iter->create_time);
		json_object_object_add(jt, "IP", j_ip);
		json_object_object_add(jt, "PORT", j_port);
		json_object_object_add(jt, "ID", j_id);
		json_object_object_add(jt, "STATUS", j_status);
		json_object_object_add(jt, "MDS_ID", j_mds_id);
		json_object_object_add(jt, "READ_WEIGHT", j_read_weight);
		json_object_object_add(jt, "WRITE_WEIGHT", j_write_weight);
		json_object_object_add(jt, "THREAD_NUM", j_thread_num);
		json_object_object_add(jt, "SOCKET_CONN_TIMEOUT", j_socket_conn_timeout);
		json_object_object_add(jt, "SOCKET_IDLE_TIMEOUT", j_socket_idle_timeout);
		json_object_object_add(jt, "SOCKET_SEND_TIMEOUT", j_socket_send_timeout);
		json_object_object_add(jt, "SOCKET_RECV_TIMEOUT", j_socket_recv_timeout);
		json_object_object_add(jt, "SOCKET_MAX_REQUEST_SIZE", j_socket_max_request_size);
		json_object_object_add(jt, "SOCKET_MAX_RESPONSE_SIZE", j_socket_max_response_size);
		json_object_object_add(jt, "CLIENT_CONN_POOL_SIZE", j_client_conn_pool_size);
		json_object_object_add(jt, "LOG_CNF_FILE", j_log_cnf_file);
		json_object_object_add(jt, "ROOT_DATA_PATH", j_root_data_path);
		json_object_object_add(jt, "CREATE_TIME", j_create_time);
		json_object_array_add(j_ds_arr, jt);
	}
	json_object_object_add(j, "DS_CNFS", j_ds_arr);
	string response = json_object_to_json_string(j);

        json_object_put(j);
        return response;	
}
